import ActionSheet, {
  ActionSheetTheme
} from 'tdesign-miniprogram/action-sheet/index';
import {
  MAIN_PAGES
} from '../../config/router.config';
import {
  getCurrentPage,
  queryParams
} from '../../utils/util';
const computedBehavior = require('miniprogram-computed').behavior

Page({
  behaviors: [computedBehavior],
  computed: {
    UserId(data) {
      return data.userInfo.userId
    }
  },
  /** 
   * 页面的初始数据
   */
  data: {
    userInfo: {
      deptId: '',
      deptName: '',
      roleId: '',
      roleName: '',
      userId: '',
      userName: '',
      phone: ''
    },

    column: [{
        icon: 'shuaxin',
        label: '切换角色',
        iconColor: '#D54941',
        iconBackgoundColor: '#FFF0ED',
        fn: 'switchRole'
      },
      {
        icon: 'dasao',
        label: '清除缓存',
        iconColor: '#E37318',
        iconBackgoundColor: '#FFF1E9',
        fn: 'clearStorage'

      },
      {
        icon: 'wenben',
        label: '隐私协议',
        iconColor: '#2BA471',
        iconBackgoundColor: '#E3F9E9',
        fn: 'clearStorage'

      }
    ]
  },
  clickColumnItem(e) {
    const {
      fn
    } = e.currentTarget.dataset
    this[fn]()
  },

  // 切换角色
  switchRole() {
    ActionSheet.show({
      theme: ActionSheetTheme.List,
      selector: '#t-action-sheet',
      context: this,
      items: [{
          label: '选项一',
        },
        {
          label: '选项二',
        },
        {
          label: '选项三',
        },
        {
          label: '选项四',
        },
        {
          label: '选项四',
        },
        {
          label: '选项四',
        },
      ],
    });
  },
  toLogin() {
    const params = {
      backTo: getCurrentPage()
    }
    wx.navigateTo({
      url: `${MAIN_PAGES.AUTH.router}${queryParams(params)}`,
    })
  },
  // 清除缓存
  clearStorage() {
    const the = this
    wx.showModal({
      title: '提示',
      content: '清除缓存后,需重新登录',
      success(res) {
        if (res.confirm) {
          try {
            wx.clearStorageSync()
            the.setData({
              'userInfo.id': null
            })
          } catch (err) {

          }
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },

  onUserInfoLoad(data) {
    const info = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: JSON.parse(info)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // if (typeof this.getTabBar === 'function' && this.getTabBar()) {
    //   this.getTabBar().setData({
    //     acitvedIndex: 4
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})